/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip;

import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sip.*;
import javax.sip.header.CallIdHeader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SipProviderImplTest extends TestCase {
    private final static Log logger = LogFactory.getLog("SipProviderImplTest");

    public SipProviderImplTest(String name) {
        super(name);
    }

    final static int PORT_NUMBER = 10000;

    public void test() {
        final SipFactory factory = SipFactory.getInstance();
        factory.setPathName("com.jnetx");
        final Properties props = new Properties();
        props.setProperty("javax.sip.STACK_NAME", "Deps SIP Stack v0.1");
        //Current IP address
        try {
            InetAddress local = InetAddress.getLocalHost();
            props.setProperty("com.jnetx.javax.sip.IP_ADDRESS", local.getHostAddress());
        } catch (UnknownHostException e) {
            final String message = "Cannot get the local host's IP address!";
            logger.error(message, e);
            fail(message);
            return;
        }
        final SipStack stack;
        try {
            stack = SipFactory.getInstance().createSipStack(props);
        } catch (PeerUnavailableException e) {
            final String message = "Cannot create SIP stack!";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created SIP stack object is null!", stack);
        ListeningPoint lPoint;
        //There is no IP address specified in the properties so the following method has to throw an exception
        try {
            lPoint = stack.createListeningPoint(PORT_NUMBER, "UDP");
            fail("The createListening method has to throw an exception if there is no IP address specified in the properties!");
        } catch (TransportNotSupportedException e) {
        } catch (InvalidArgumentException e) {
            final String message = "Cannot create a listening point!";
            logger.error(message, e);
            fail(message);
            return;
        }
        try {
            lPoint = stack.createListeningPoint(getLocalIPAddress(), PORT_NUMBER, "UDP");
        } catch (Exception e) {
            final String message = "";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created listening point is null!", lPoint);
        final SipProvider provider;
        try {
            provider = stack.createSipProvider(lPoint);
        } catch (ObjectInUseException e) {
            final String message = "Cannot create a SIP provider!";
            logger.error(message, e);
            fail(message);
            return;
        }
        assertNotNull("The created provider object is null!", provider);

        CallIdHeader callId = provider.getNewCallId();
        System.out.println("call id: " + callId);
        assertNotNull("The callId header is null!", callId);
        assertTrue("The call id should be entailed by local address!", callId.getCallId().endsWith("@" + getLocalIPAddress()));

        try {
            stack.deleteSipProvider(provider);
        } catch (ObjectInUseException e) {
            final String message = "Cannot delete the SIP provider!";
            logger.error(message, e);
            fail(message);
        }
        try {
            stack.deleteListeningPoint(lPoint);
        } catch (ObjectInUseException e) {
            final String message = "Cannot delete the listening point!";
            logger.error(message, e);
            fail(message);
        }
    }

    private static String getLocalIPAddress() {
        final String result;
        //Current IP address
        try {
            InetAddress local = InetAddress.getLocalHost();
            result = local.getHostAddress();
        } catch (UnknownHostException e) {
            logger.error("Cannot get the local host's IP address!", e);
            fail("Cannot get the current IP address!");
            return null;
        }
        return result;
    }
}
/*
 * $Log$
 */